<?php
namespace Yzadmin\Controller;
use Think\Controller;
class LoginController extends Controller
{
   
   //后台首页
   public function index()
   {
     //session读取
     $username=cookie('admin_username');
     if(isset($username))
      { 
        //session(null);
        $this->success("已登陆!自动跳转后台首页",U("Index/index"));
        die();
      }
     $this->display('./login');
   }

   //后台登陆设置
   public function logins()
   {
     $data['username'] = I('post.username');
     $data['password'] = md5(I('post.password'));
     if(empty($data['username']) || empty($data['password']))
     {
       $this->error("麻烦你老先把信息写全在登陆!");
     }
     $muser=M("admin");//实例化数据库
     $user=$muser->where($data)->find();
     if(!$user)
     {
       $this->error("账号或者用户名错误!请重新登录!");
     }else
     { 
       $updata=array(
        "ip"=>get_client_ip(),
        "logintime"=>time() 
        );
       //更新IP和登陆时间记录
       if($muser->where('id=1')->save($updata))
       {
         //$this->success("登陆成功!");
         cookie('admin_username',$user['username'],(3600*24)*7);
         cookie('admin_id',$user['id'],(3600*24)*7);
         $this->success("登陆成功",U("Index/index"));
       }
     }
   } 
   
   /**
    * 退出登陆
    */
   public function logout()
   {
     cookie('admin_username',null);
     $this->success("退出成功",U("Login/index"));
   }
    
   //空操作
   public function _empty()
   {
     die("404");
   }

}